﻿<!DOCTYPE HTML>
<html lang="zh">
<head>
<title>随机数 - 语法 &amp; 使用 | AutoHotkey v2</title>
<meta name="description" content="The 随机数 function generates a pseudo-random number." />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
<script type="text/javascript">$(function(){0<=window.navigator.userAgent.toLowerCase().indexOf("ucbrowser")&&CaoNiMaDeUc()})</script>
</head>
<body>

<h1>随机数</h1>

<p>生成一个伪随机数字.</p>

<pre class="Syntax">N := <span class="func">随机数</span>(<span class="optional">最小值, 最大值</span>)</pre>
<h2 id="Parameters">参数</h2>
<dl>

  <dt>最小值</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#numbers">整数</a>或<a href="../Concepts.htm#numbers">浮点数</a></p>
    <p>可以生成的最小数字, 可以是负数或浮点数. 如果省略, 最小的数字默认为 0. 允许的最小整数值为 -2147483648, 但浮点数没有限制.</p>
  </dd>

  <dt>最大值</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#numbers">整数</a>或<a href="../Concepts.htm#numbers">浮点数</a></p>
    <p>可以生成的最小数字, 可以是负数或浮点数. 如果省略, 最大的数字默认为 2147483647(这也是允许的最大整数值 -- 但浮点数没有限制).</p>
  </dd>

</dl>

<h2 id="Return_Value">返回值</h2>
<p>类型: <a href="../Concepts.htm#numbers">整数</a>或<a href="../Concepts.htm#numbers">浮点数</a></p>
<p>这个函数返回一个伪随机生成的数字, 这是一个模拟真实随机数的数字, 但实际上是一个基于复杂公式的数字, 使确定/猜测下一个数字变得非常困难.</p>

<h2 id="Remarks">备注</h2>
<p>在指定范围中所有数字被生成的机率几乎是相同的(然而, 请参阅下面的 &quot;已知限制&quot;).</p>
<p>如果 <em>最小值</em> 或 <em>最大值</em> 其中一个为小数, 则最终结果将为浮点数. 否则, 结果为整数.</p>
<p>浮点数的已知限制: 1) 对于任何特定的范围, 只能生成大约 4,294,967,296 个不同的数字, 所以该范围内的所有其他数字将永远不会生成; 2) 偶尔结果可能略大于指定的 <em>最大值</em>(这种情况是由浮点数固有的不精确性导致的).</p>

<h2 id="Seed">设置随机数种子</h2>
<p>使用 <em>NewSeed</em> 重新设定随机数生成器的种子.</p>
<pre class="Syntax"><span class="func">设置随机数种子</span> NewSeed</pre>
<p>这将影响所有后续生成的随机数. <em>NewSeed</em> 应该是 0 到 4294967295(0xFFFFFFFF) 之间的整数. Reseeding 可以提高产生的随机数的质量/安全性, 尤其当 <em>NewSeed</em> 是真正的随机数而不是质量不佳的伪随机数时. 通常, reseeding does not need to be done more than once.</p>
<p>如果脚本从未重新设定种子, 则种子使用自 1601 年 1 月 1 日以来 100 纳秒间隔数目的 64 位值的低 32 位来开始. 这个值每 ~7.2 分钟内从 0 变化到 4294967295.</p>

<h2 id="Examples">示例</h2>
<div class="ex" id="ExBasic">
<p><a href="#ExBasic">#1</a></p>
<pre>number := 随机数(1, 10)
fraction := 随机数(0.0, 1.0)</pre>
</div>

<h2>基于原始来源的注释</h2>
<p>该函数使用了 Mersenne Twister(马特赛特旋转演算法) 随机数发生器, MT19937, 由 Takuji Nishimura, Makoto Matsumoto, Shawn Cokus, Matthe Bellew 和 Isaku
  Wada 编写.</p>
<p>马特赛特旋转演算法是一种生成伪随机数的算法. 它是考虑到其他各种发生器的缺陷而设计的. 周期
  2<sup>19937</sup>-1, 623 维均匀分布, 足够大, 速度快; 该算法避免了乘除法, 它有益于缓存和管道. 更多信息访问开发者网页 <a href="http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html">www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html</a></p>
<p>Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, All rights
  reserved.</p>
<p>Redistribution and use in source and binary forms, with or without modification,
  are permitted provided that the following conditions are met:</p>
<ol>
  <li>Redistributions of source code must retain the above copyright notice, this
    list of conditions and the following disclaimer.</li>
  <li>Redistributions in binary form must reproduce the above copyright notice,
    this list of conditions and the following disclaimer in the documentation
    and/or other materials provided with the distribution.</li>
  <li>The names of its contributors may not be used to endorse or promote products
    derived from this software without specific prior written permission.</li>
</ol>
<p class="J">THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  &quot;AS IS&quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 遍历 A PARTICULAR PURPOSE
  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  遍历 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  EVEN 如果 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</p>
<p>Do NOT use 遍历 CRYPTOGRAPHY without securely hashing several returned values
  together, otherwise the generator state can be learned after reading 624 consecutive
  values.</p>
<p>版权所有(C) 1997 - 2002, Makoto Matsumoto 和 Takuji Nishimura, 保留所有权利.</p>
<p>在满足以下条件的情况下, 允许以源码和二进制形式进行再分发和使用, 无论是否修改:</p>
<ol>
  <li>源代码的再分发必须保留上述版权声明, 本条件清单和以下免责声明.</li>
  <li>二进制形式的再分发必须在文档和/或其他随发行版提供的材料中复制上述版权声明, 本条件列表和以下免责声明.</li>
  <li>未经特定的事先书面许可, 不得使用其贡献者的名字来认可或推广本软件的衍生产品.</li>
</ol>
<p class="J-2">THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  &quot;AS IS&quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 遍历 A PARTICULAR PURPOSE
  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  遍历 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  EVEN 如果 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</p>
<p>在不安全地将几个返回值散列在一起的情况下, 不要用于加密, 否则在读取 624 个连续值后, 可以了解生成器的状态.</p>
<p>当你使用这个功能时, 请发邮件到: m-mat@math.sci.hiroshima-u.ac.jp 并适当注明你的作品.</p>
<p><i>以上这些在 AutoHotkey 中已经完成, 但如果你在公开发布的应用中使用了 随机数 函数, 可以考虑给上面的人发邮件表示感谢.</i></p>

</body>
</html>